package com.sixseven.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sixseven.bean.Cart;
import com.sixseven.bean.Member;
import com.sixseven.bean.Order;
import com.sixseven.bean.OrderItem;
import com.sixseven.service.OrderItemService;
import com.sixseven.service.OrderService;
import com.sixseven.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Belly
 * @version 1.1.0
 */
@RestController
@Slf4j
public class OrderController {
    @Resource
    private OrderService orderService;
    @Resource
    private OrderItemService orderItemService;

    @RequestMapping("/addOrder")
    public Result addOrder(HttpServletRequest request) {
        Member member = (Member) request.getSession().getAttribute("member");
        if (member == null) {
            return Result.error("486", "请先登录", null);
        }
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        if (cart == null) {
            return Result.error("487", "购物车空空如也", null);
        }
        String orderID = orderService.saveOrder(member.getId(), cart);
        return orderID != null ? Result.success(orderID) : Result.error("487", "购物车空空如也", null);
    }
    @RequestMapping("/orderList")
    public Result orderList(HttpServletRequest request){
        Member member = (Member) request.getSession().getAttribute("member");
        if (member == null){
            return Result.error();
        }
        LambdaQueryWrapper<Order> orderLambdaQueryWrapper = Wrappers.lambdaQuery();
        orderLambdaQueryWrapper.eq(Order::getMemberId, member.getId());
        List<Order> orderList = orderService.list(orderLambdaQueryWrapper);
        if (orderList == null){
            return Result.success("222", "你还未下单");
        }
        return Result.success(orderList);
    }
    @RequestMapping("/orderItemList/{orderId}")
    public Result orderItemList(@PathVariable String orderId) {
        if (orderId.isEmpty()) {
            return Result.error();
        }
        LambdaQueryWrapper<OrderItem> orderItemLambdaQueryWrapper = Wrappers.lambdaQuery();
        orderItemLambdaQueryWrapper.eq(OrderItem::getOrderId, orderId);
        List<OrderItem> orderItemList = orderItemService.list(orderItemLambdaQueryWrapper);
        return Result.success(orderItemList);
    }
    @RequestMapping("/orderAllList")
    public Result orderAllList(@RequestParam(defaultValue = "") String searchName,
                               @RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "3") Integer pageSize){
        LambdaQueryWrapper<Order> orderLambdaQueryWrapper = Wrappers.lambdaQuery();
        orderLambdaQueryWrapper.like(Order::getId, searchName);
        Page<Order> orderPage = orderService.page(
                new Page<>(pageNum, pageSize), orderLambdaQueryWrapper);
//        List<Order> orderList = orderService.list(orderLambdaQueryWrapper);
        if (orderPage == null){
            return Result.success("010", "订单消失辣");
        }
        return Result.success(orderPage);
    }
    @RequestMapping("/changeStatus")
    public Result changeOrderStatus(@RequestParam String orderId, @RequestParam Integer status){
        if (orderId.isEmpty()){
            return Result.error();
        }
        if (status == null){
            return Result.error();
        }
        Order order = orderService.getOne(new LambdaQueryWrapper<Order>().eq(Order::getId, orderId));
        if (order == null){
            return Result.error();
        }
        order.setStatus(status);
        boolean update = orderService.updateById(order);
        if (!update){
            return Result.error();
        }
        return Result.success("200", "状态更改成功");
    }
    @DeleteMapping("/delOrder/{orderId}")
    public Result delOrder(@PathVariable String orderId){
        if (orderId == null){
            return Result.error();
        }
        boolean removeById = orderService.removeById(orderId);
        if (removeById){
            return Result.success("200", "删除成功");
        }
        return Result.error();
    }
}
